package com.share.filter;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * 拦截器  日志拦截写入文件
 * @author 小赵
 * @version 1.0
 * @data 2024-12-12 16:05
 */

@Slf4j
@Component
public class LoggerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("Pre Handle method is called at {}", LocalDateTime.now());
        LocalDateTime requestTime = LocalDateTime.now();
        String logEntry = String.format(
                "请求信息 - 时间: %s, URI: %s, 请求类型: %s, 请求IP: %s",
                requestTime,
                request.getRequestURI(),
                request.getMethod(),
                request.getRemoteAddr()
        );

        log.info(logEntry);
        request.setAttribute("requestTime", requestTime);
        request.setAttribute("user_id", request.getHeader("user_id"));
        request.setAttribute("username", request.getHeader("username"));

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        LocalDateTime handleTime = LocalDateTime.now();
        String logEntry = String.format(
                "处理中 - 处理时间: %s, URI: %s, 请求类型: %s, 请求IP: %s",
                handleTime,
                request.getRequestURI(),
                request.getMethod(),
                request.getRemoteAddr()
        );

        log.info(logEntry);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LocalDateTime requestTime = (LocalDateTime) request.getAttribute("requestTime");
        LocalDateTime handleTime = LocalDateTime.now();
        LocalDateTime responseTime = LocalDateTime.now();

        String userId = (String) request.getAttribute("user_id");
        String username = (String) request.getAttribute("username");

        String logEntry = String.format(
                "响应信息 - 请求时间: %s, 处理时间: %s, 响应时间: %s, 响应状态码: %d, 用户ID: %s, 用户名: %s, 请求IP: %s, 请求URL: %s, 请求类型: %s",
                requestTime,
                handleTime,
                responseTime,
                response.getStatus(),
                userId != null ? userId : "未提供",
                username != null ? username : "未提供",
                request.getRemoteAddr(),
                request.getRequestURI(),
                request.getMethod()
        );

        if (response.getStatus() == 200) {
            log.info("[SUCCESS] " + logEntry); // 表示成功
        } else {
            log.error("[ERROR] " + logEntry); // 表示错误
        }

        if (ex != null) {
            log.error("异常信息: {}", ex.getMessage());
        }
    }
}
